package com.ruoyi.traces.controller;

import com.github.pagehelper.PageInfo;
import com.ruoyi.common.constant.HttpStatus;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.domain.model.LoginUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.traces.domain.TraceTransport;
import com.ruoyi.traces.domain.TraceUserCrops;
import com.ruoyi.traces.domain.TraceUserMaterial;
import com.ruoyi.traces.service.MaterialService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("/traces/material")
public class MaterialController extends BaseController {
    @Autowired
    private MaterialService materialService;
    /**
     * 查询用户产品总数
     */
    @GetMapping("/count")
    public int count()
    {
        List<TraceTransport> list = new ArrayList<TraceTransport>();
        list = materialService.getList();
        return list.size();
    }

    @GetMapping("/materialList/{deptId}")
    public AjaxResult getMaterialList(@PathVariable("deptId") String deptId){
        return AjaxResult.success(materialService.getMaterialList(deptId));
    }
    @GetMapping("/list/{deptId}")
    public TableDataInfo getListByDeptId(@PathVariable("deptId") String deptId) {
        LoginUser loginuser = SecurityUtils.getLoginUser();
        SysUser sysUser = loginuser.getUser();
        startPage();
        List<TraceTransport> list = new ArrayList<TraceTransport>();
        if(SecurityUtils.isAdmin(sysUser.getUserId())){
            list = materialService.getList();
        }
        else{
            list = materialService.getMaterialList(deptId);
        }
        return getDataTable(list);
    }
    /**
     * @author wx
     * @Date 23:47 2020/11/7 0007
     * @Param [cropsId] 出库
     * @return com.ruoyi.common.core.domain.AjaxResult
     **/
    @GetMapping("/changeInToOut/{cropsId}")
    public AjaxResult changeInToOut(@PathVariable("cropsId") String cropsId){
        return AjaxResult.success(materialService.changeInToOut(cropsId));
    }

    @PostMapping("/addTransport")
    public AjaxResult addTransport(@RequestBody TraceTransport traceTransport){
        return AjaxResult.success(materialService.addTransport(traceTransport));
    }

    /**
     * 响应请求分页数据
     */
    @SuppressWarnings({ "rawtypes", "unchecked" })
    protected TableDataInfo getDataTable(List<?> list)
    {
        TableDataInfo rspData = new TableDataInfo();
        rspData.setCode(HttpStatus.SUCCESS);
        rspData.setMsg("查询成功");
        rspData.setRows(list);
        rspData.setTotal(new PageInfo(list).getTotal());
        return rspData;
    }

}
